<template>
	<div class="card__body-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<el-form v-show="state.showSearch" :model="state.queryParams" ref="queryFormRef" inline>
				<el-form-item label="品牌名称" prop="name">
					<el-input v-model="state.queryParams.name" placeholder="请输入品牌名称" clearable />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="onQuery">
						<el-icon>
							<ele-Search />
						</el-icon>
						查 询
					</el-button>
					<el-button @click="onResetQuery">
						<el-icon>
							<ele-Refresh />
						</el-icon>
						重 置
					</el-button>
				</el-form-item>
			</el-form>
			<el-row :gutter="10" class="top-block">
				<el-col :span="1.5">
					<el-button type="primary" @click="onOperation({ type: 'add', title: '添加商品品牌' }, '')"> 添加商品品牌 </el-button>
				</el-col>
				<RightToolbar v-model:showSearch="state.showSearch" @queryTable="getList" />
			</el-row>
			<el-table :data="state.tableList" v-loading="state.loading">
				<el-table-column label="编号" align="center" prop="id" />
				<el-table-column label="品牌名称" align="center" prop="name" />
				<el-table-column label="状态" align="center" prop="status">
					<template #default="{ row }">
						<el-switch
							:before-change="beforeSwitchChange"
							@change="onStatusChange(row)"
							v-model="row.status"
							:active-value="1"
							:inactive-value="0"
							inline-prompt
							active-text="启用"
							inactive-text="禁用"
							style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
						/>
					</template>
				</el-table-column>
				<el-table-column label="排序" align="center" prop="display_order" />
				<el-table-column label="创建时间" align="center" prop="create_time" :formatter="formatterCreateTime" />
				<el-table-column label="操作" align="center" prop="operation" fixed="right">
					<template #default="{ row }">
						<el-button type="primary" link @click="onOperation({ type: 'edit', title: '修改商品品牌' }, row)"> 修改 </el-button>
						<el-button type="primary" link @click="onTranslate(row)"> 翻译管理 </el-button>
						<el-popconfirm title="您确定删除该数据？此操作不可撤回" @confirm="onDelete(row)" width="180px">
							<template #reference>
								<el-button type="primary" link> 删除 </el-button>
							</template>
						</el-popconfirm>
					</template>
				</el-table-column>
			</el-table>
			<PaginationComponent
				v-if="state.total"
				:total="state.total"
				v-model:page="state.queryParams.pageNum"
				v-model:limit="state.queryParams.pageSize"
				@pagination="getList"
			/>
		</el-card>
		<OperationDialog ref="operationDialogRef" />
		<TranslateDialog ref="translateDialogRef" />
	</div>
</template>

<script setup lang="ts" name="goodBrand">
import { ElMessage, ElMessageBox } from 'element-plus';
import { defineAsyncComponent, onMounted, reactive, ref } from 'vue';
import { goodsBrandApi } from '/@/api';
import { timeDictionary } from '/@/utils/dictionary';

const OperationDialog = defineAsyncComponent(() => import('./dialog/operationDialog.vue'));
const TranslateDialog = defineAsyncComponent(() => import('./dialog/translateDialog.vue'));

const GoodsBrandApi = goodsBrandApi();
// 定义变量内容
const state = reactive({
	showSearch: true,
	queryParams: {
		name: '',
		pageNum: 1,
		pageSize: 10,
	},
	tableList: [],
	loading: false,
	total: 0,
	switchStatus: false,
});
const queryFormRef = ref();
const operationDialogRef = ref();
const translateDialogRef = ref();

onMounted(() => {
	getList();
});

/********** 触发事件 **********/
// 查询
const onQuery = () => {
	state.queryParams.pageNum = 1;
	getList();
};
// 重置查询
const onResetQuery = () => {
	queryFormRef.value.resetFields();
	onQuery();
};
// 添加/修改
const onOperation = (data: Object, row: Object) => {
	operationDialogRef.value.openDialog(data, row, () => getList());
};
// 翻译管理
const onTranslate = (row: Object) => {
	translateDialogRef.value.openDialog(row);
};
const beforeSwitchChange = () => {
	state.switchStatus = true;
	return state.switchStatus;
};
// 启用/禁用
const onStatusChange = (row: any) => {
	if (!state.switchStatus) return;
	ElMessageBox.confirm(`您确定${row.status == 1 ? '启用' : '禁用'}该数据项？`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			return GoodsBrandApi.goodsBrandEdit({ ...row, goods_brand_id: row.id });
		})
		.then(() => {
			ElMessage.success('操作成功！');
		})
		.catch(() => {
			row.status = row.status ? 0 : 1;
		});
};
// 删除
const onDelete = (row: any) => {
	_goodsBrandDelete({ goods_brand_id: row.id });
};

/********** 方法事件 **********/
// 刷新列表
const getList = () => {
	state.loading = true;
	_goodsBrandList(state.queryParams);
};
// 创建时间字典
const formatterCreateTime = (row: any) => {
	return timeDictionary(row.create_time);
};

/********** 网络事件 **********/
// 商品品牌列表
const _goodsBrandList = (params: Object) => {
	GoodsBrandApi.goodsBrandList(params)
		.then((res) => {
			state.tableList = res.data.list;
			state.total = res.data.info.total;
			state.loading = false;
		})
		.catch(() => (state.loading = false));
};
// 删除商品品牌
const _goodsBrandDelete = (params: Object) => {
	GoodsBrandApi.goodsBrandDelete(params).then(() => {
		ElMessage.success('操作成功！');
		getList();
	});
};
</script>
